package com.wechat.page.login;

import java.util.List;

import com.swak.fx.support.AbstractComp;
import com.swak.fx.support.AbstractPage;
import com.swak.fx.support.Display;
import com.swak.fx.support.FXMLView;
import com.swak.fx.support.ui.UI;
import com.swak.fx.support.ui.UIButton;
import com.swak.fx.support.ui.UIIcon;
import com.swak.fx.support.ui.UIImage;
import com.swak.utils.Lists;
import com.swak.utils.StringUtils;
import com.wechat.login.Login;
import com.wechat.login.LoginService;

import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;

/**
 * 
 * 切换用户
 * 
 * @author 618lf
 */
@FXMLView(title = "切换用户", value = "UserSwitchComp.fxml", css = "UserSwitchComp.css", stageStyle = "TRANSPARENT")
public class UserSwitchComp extends AbstractComp {

	@FXML
	private UIImage image;
	@FXML
	private Label name;
	@FXML
	private Text createBtn;
	@FXML
	private Pane placeholder;
	@FXML
	private UIIcon leftBtn;
	@FXML
	private UIIcon rightBtn;
	@FXML
	private UIButton confirmBtn;

	/**
	 * 列表选择
	 */
	private IntegerProperty indexProperty = new SimpleIntegerProperty();
	private ListProperty<Login> usersProperty = new SimpleListProperty<>(FXCollections.observableArrayList());

	public UserSwitchComp(AbstractPage parent) {
		super(parent);
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		this.image.image(Display.logo());
		this.confirmBtn.Success().setText("确定");
		this.leftBtn.Icon(UI.Icons.LEFT).setPrefWidth(40);
		this.leftBtn.Round().size(18);
		this.rightBtn.Icon(UI.Icons.RIGHT).setPrefWidth(40);
		this.rightBtn.Round().size(18);
	}

	@Override
	public void setupEventHandlers() {
		super.setupEventHandlers();
		this.createBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			this.$emitInPage("page:register");
		});
		this.leftBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			this.selectPrev();
		});
		this.rightBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
			this.selectNext();
		});
		this.confirmBtn.setOnAction((e) -> {
			int index = this.indexProperty.get();
			int size = this.usersProperty.getSize();
			if (index >= 0 && index < size) {
				Login login = this.usersProperty.get(index);
				if (login != null) {
					try {
						LoginService.me().select(login.getId());
					} catch (Exception e2) {
					}
					this.$emitInPage("page:login");
				}
			}
		});
	}

	private void selectPrev() {
		int index = this.indexProperty.get();
		int size = this.usersProperty.getSize();
		int prev = index - 1;
		if (prev >= 0 && prev < size) {
			this.indexProperty.set(prev);
		}
	}

	private void selectNext() {
		int index = this.indexProperty.get();
		int size = this.usersProperty.getSize();
		int next = index + 1;
		if (next >= 0 && next < size) {
			this.indexProperty.set(next);
		}
	}

	@Override
	public void setupBindings() {
		super.setupBindings();
		this.leftBtn.visibleProperty().bind(Bindings.createBooleanBinding(() -> {
			int index = this.indexProperty.get();
			return index == 0 ? false : true;
		}, this.indexProperty, this.usersProperty));
		this.rightBtn.visibleProperty().bind(Bindings.createBooleanBinding(() -> {
			int index = this.indexProperty.get();
			int size = this.usersProperty.getSize();
			return index < size - 1 ? true : false;
		}, this.indexProperty, this.usersProperty));
		this.name.textProperty().bind(Bindings.createStringBinding(() -> {
			int index = this.indexProperty.get();
			int size = this.usersProperty.getSize();
			if (index >= 0 && index < size) {
				Login login = this.usersProperty.get(index);
				return login != null ? login.getNickName() : StringUtils.EMPTY;
			}
			return StringUtils.EMPTY;
		}, this.indexProperty, this.usersProperty));
		this.image.nodeProperty().bind(Bindings.createObjectBinding(() -> {
			int index = this.indexProperty.get();
			int size = this.usersProperty.getSize();
			if (index >= 0 && index < size) {
				Login login = this.usersProperty.get(index);
				return login != null && login.getHeadimg() != null ? login.getHeadimg() : Display.logo();
			}
			return Display.logo();
		}, this.indexProperty, this.usersProperty));
	}

	@Override
	public void setupValueChangedListeners() {
		super.setupValueChangedListeners();
	}

	@Override
	public void activated() {
		super.activated();
		this.usersProperty.clear();
		List<Login> users = null;
		try {
			users = LoginService.me().users();
		} catch (Exception e) {
			users = Lists.newArrayList();
		}
		this.usersProperty.addAll(users);
	}
}
